package com.dx.wx.controller;

import com.dx.wx.service.DxHomeService;
import com.dx.wx.service.DxOrderService;
import com.dx.wx.service.DxUserService;
import com.ejlchina.data.Array;
import com.ejlchina.data.Mapper;
import com.yf.annotation.LoginUserId;
import com.yf.response.R;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Map;

@RestController
@RequestMapping("/api/dx/order")
public class ApiDxOrderController {

    @Autowired
    private DxOrderService dxOrderService;

    @PostMapping("/")
    public R saveOrder(@LoginUserId Long phone, @RequestBody Map data){
        dxOrderService.save(phone,data);
        return R.ok();
    }


    @Autowired
    private DxHomeService dxHomeService;

    @Autowired
    private DxUserService dxUserService;

    @GetMapping("/gift")
    public R gift(@LoginUserId Long phone){
        Mapper mapper = dxHomeService.gift();
        Long giftDate = mapper.getLong("field_qx6tP__c");
        Mapper user = dxUserService.findByMobile(String.valueOf(phone));
        Long userDate  =  user.getLong("create_time");
        if(userDate<giftDate){ //活动之前注册的
            return R.ok(true);
        }
        Array array = dxOrderService.findByType("record_FL122__c",String.valueOf(phone));
        return R.ok(array.size());
    }


    //消费者晒单
    @PostMapping("/cus")
    public R saveCusOrder(@LoginUserId Long phone, @RequestBody Map data){
        dxOrderService.saveCus(phone,data);
        return R.ok();
    }

    @GetMapping("/cus")
    public R listCusOrder(@LoginUserId Long phone, Integer type,Long startDate,Long endDate){
        Array array = dxOrderService.findCusByPhone(phone,startDate,endDate,type);
        return R.ok(array.toString());
    }

    /**
     * 晒单查询
     * type 0 查询自己的  1 查询下级的
     * @param phone
     * @return
     */
    @GetMapping("/")
    public R listOrder(@LoginUserId Long phone, Integer type,Long startDate,Long endDate){
       Array array = dxOrderService.findByPhone(phone,startDate,endDate,type);
       return R.ok(array.toString());
    }


    /**
     * 晒单明细查询
     * type 0 查询自己的  1 查询下级的
     * @param phone
     * @return
     */
    @GetMapping("/list")
    public R listOrderList(@LoginUserId Long phone, Integer type,@RequestParam(required = false) Long startDate,@RequestParam(required = false) Long endDate){
        Array array = dxOrderService.findListByPhone(phone,startDate,endDate,type);
        return R.ok(array.toString());
    }





}
